package ssl

import (
	"errors"
	"time"

	cas20200407 "github.com/alibabacloud-go/cas-20200407/v2/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	"github.com/istower/ssl_cert_auto/utils"
)

var (
	sslClient *cas20200407.Client
	_err      error
)

type CertificateInfo struct {
	Domain       string `json:"Domain,omitempty"`
	ProductCode  string `json:"product_code,omitempty"`
	Username     string `json:"username,omitempty"`
	Phone        string `json:"phone,omitempty"`
	Email        string `json:"email,omitempty"`
	ValidateType string `json:"validate_type,omitempty"`
	OrderID      int64  `json:"order_id,omitempty"`
	Times        uint8  `json:"times,omitempty"`
	StorePath    string `json:"store_path,omitempty"`
}

func Init(ak *utils.AccessKey) {
	sslClient, _err = utils.GeneratedSSlClient(tea.String(ak.AccessKeyID), tea.String(ak.AccessKeySecret))
	if _err != nil {
		utils.Log.Fatalf("连接阿里云失败,错误信息: %s", _err)
	}
}

func NewCertificateInfo(domain string, productCode string, validateType string, path string) *CertificateInfo {
	return &CertificateInfo{Domain: domain, ProductCode: productCode, ValidateType: validateType, StorePath: path}
}

func (ci *CertificateInfo) CreateCertificate() (*cas20200407.CreateCertificateForPackageRequestResponse, error) {
	createCertificateForPackageRequestRequest := &cas20200407.CreateCertificateForPackageRequestRequest{
		Domain:       tea.String(ci.Domain),
		ProductCode:  tea.String(ci.ProductCode),
		Username:     tea.String(ci.Username),
		Phone:        tea.String(ci.Phone),
		Email:        tea.String(ci.Email),
		ValidateType: tea.String(ci.ValidateType),
	}
	runtime := &util.RuntimeOptions{}
	res, tryErr := func() (res *cas20200407.CreateCertificateForPackageRequestResponse, _e error) {
		defer func() {
			if r := tea.Recover(recover()); r != nil {
				_e = r
			}
		}()
		// 复制代码运行请自行打印 API 的返回值
		res, _err := sslClient.CreateCertificateForPackageRequestWithOptions(createCertificateForPackageRequestRequest, runtime)
		if _err != nil {
			return nil, _err
		}
		return res, nil
	}()

	if tryErr != nil {
		var e = &tea.SDKError{}
		if _t, ok := tryErr.(*tea.SDKError); ok {
			e = _t
		} else {
			e.Message = tea.String(tryErr.Error())
		}
		// 如有需要，请打印 error
		_, _err = util.AssertAsString(e.Message)
		if _err != nil {
			return nil, _err
		}
		return nil, errors.New(*e.Message)
	}
	return res, _err
}

func (ci *CertificateInfo) DescribeCertificateState() (*cas20200407.DescribeCertificateStateResponse, error) {
	time.Sleep(time.Second * 1)
	describeCertificateStateRequest := &cas20200407.DescribeCertificateStateRequest{
		OrderId: tea.Int64(ci.OrderID),
	}
	runtime := &util.RuntimeOptions{}
	res, tryErr := func() (res *cas20200407.DescribeCertificateStateResponse, _e error) {
		defer func() {
			if r := tea.Recover(recover()); r != nil {
				_e = r
			}
		}()
		// 复制代码运行请自行打印 API 的返回值
		res, _err = sslClient.DescribeCertificateStateWithOptions(describeCertificateStateRequest, runtime)
		if _err != nil {
			return nil, _err
		}

		return res, nil
	}()

	if tryErr != nil {
		var e = &tea.SDKError{}
		if _t, ok := tryErr.(*tea.SDKError); ok {
			e = _t
		} else {
			e.Message = tea.String(tryErr.Error())
		}
		// 如有需要，请打印 error
		_, _err = util.AssertAsString(e.Message)
		if _err != nil {
			return nil, _err
		}
	}
	return res, _err
}
